package com.zjitc.token;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.junit.Test;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/2 15:12
 * @description:
 */
public class Cache {
  private Map<String,Token> tokens= new HashMap<>();

  public void add(String username,String uuid){
    tokens.put(username,new Token(uuid, LocalDateTime.now()));
  }

  public Token get(String username){
    return tokens.get(username);
  }

  public Set<Entry<String,Token>> getAllTokens(){
    return tokens.entrySet();
  }

  public static void main(String[] args){
    Cache cache = new Cache();
    cache.add("a","a");
    cache.add("b","b");
    cache.add("c","c");
    //ExecutorService executorService = Executors.newSingleThreadExecutor();

    ScheduledExecutorService service=Executors.newScheduledThreadPool(1);
    System.out.println("当前时间："+LocalDateTime.now());
    service.scheduleAtFixedRate(()->{
      System.out.println("第一次运行的时间："+LocalDateTime.now());
      while(true){

        Set<Entry<String,Token>> sets=cache.getAllTokens();
        for (Entry<String,Token> entry:sets){
          String key=entry.getKey();
          Token token=entry.getValue();
          LocalDateTime creatTime=token.getCreateTime();
          LocalDateTime nowTime=LocalDateTime.now();
          Duration between = Duration.between(creatTime,nowTime);
          if (between.abs().toMillis()>5000){
            System.out.println(token.getUuid());
          }
        }
      }
  },1000,1000, TimeUnit.MILLISECONDS);

    Scanner scanner = new Scanner(System.in);
    scanner.next();
  }

  @Test
  public void druing(){
    LocalDateTime t1=LocalDateTime.now();
    try {
      Thread.sleep(1000);
    }catch (InterruptedException e){
      e.printStackTrace();
    }
    LocalDateTime t2 = LocalDateTime.now();
    System.out.println(t1);
    System.out.println(t2);
    Duration between = Duration.between(t1,t2);
    System.out.println(between.abs().toMillis());
  }

  @Test
  public  void  json(){
    String value="{'serverinfo':[{'data1':13},{'data2':23}]}";
    Gson gson=new GsonBuilder().create();
    B b= gson.fromJson(value,B.class);
  }

  public class B{
    private List<A> serverinfo;

    public List<A> getServerinfo() {
      return serverinfo;
    }

    public void setServerinfo(List<A> serverinfo) {
      this.serverinfo = serverinfo;
    }
  }

 public class A{
    private Integer data1;
   private Integer data2;

   public Integer getData1() {
     return data1;
   }

   public void setData1(Integer data1) {
     this.data1 = data1;
   }

   public Integer getData2() {
     return data2;
   }

   public void setData2(Integer data2) {
     this.data2 = data2;
   }
 }
}
